home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / MSDOS / msubs.asm < prev    next >
Assembly Source File  |  1979-12-31  |  10KB  |  601 lines

  1. ;------------------------------ msubs.asm ------------------------------------
  2. ;
  3. ; Math routines to support fly8.c.
  4. ;
  5. ; int far fmula (int x, int y);
  6. ;
  7. ; int far fdiva (int x, int y);
  8. ;
  9. ; int far muldiva (int x, int y, int z);
  10. ;
  11. ; int far dithadj (int x, int dither, int interval);
  12. ;
  13. ; void far Vmula (int R[3], int V[3], int M[3][3]);
  14. ;
  15. ; void far matyxz (int far T[3][3], int sx, int cx, int sy, int cy,
  16. ;    int sz, int cz)
  17. ;
  18. ; void far matxyz (int far T[3][3], int spitch, int cpitch, int sroll, 
  19. ;    int croll, int syaw, int cyaw)                 ***OBSOLETE***
  20. ;
  21. ; int chksum (int *buf, int len);
  22. ;
  23. ;-----------------------------------------------------------------------------
  24. ;
  25.     .MODEL    LARGE
  26.     .286
  27. ;
  28.     EXTRN    _sin_tab:WORD
  29. ;
  30.     .CODE
  31. ;
  32. fmulm    MACRO    a,b
  33.     mov    ax,a
  34.     imul    WORD PTR b
  35.     shl    ax,1
  36.     rcl    dx,1
  37.     shl    ax,1
  38.     rcl    dx,1
  39.     ENDM
  40. ;
  41.     PUBLIC @fmula
  42. @fmula    PROC  far
  43. ;
  44. ; int far _fastcall fmula (int x, int y); [ax, dx]
  45. ;
  46. ;    compute (x*y) >> 14
  47. ;
  48.     imul    dx
  49.     shl    ax,1
  50.     rcl    dx,1
  51.     shl    ax,1
  52.     rcl    dx,1
  53.     mov    ax,dx
  54. ;
  55.     ret
  56. @fmula    ENDP
  57. ;
  58.     PUBLIC _fmula
  59. _fmula    PROC  far
  60. ;
  61. ; int far fmula (int x, int y);
  62. ;
  63. ;    compute (x*y) >> 14
  64. ;
  65. px    EQU    ss:WORD PTR[BX+4]
  66. py    EQU    ss:WORD PTR[BX+6]
  67. ;
  68.     mov    bx,sp
  69. ;
  70.     fmulm    px,py
  71.     mov    ax,dx
  72. ;
  73.     ret
  74. _fmula    ENDP
  75. ;
  76.     PUBLIC @fdiva
  77. @fdiva    PROC  far
  78. ;
  79. ; int far _fastcall fdiva (int x, int y); [ax, dx]
  80. ;
  81. ;    compute (x/y) as a fraction: (x<<14)/y.
  82. ;
  83.     mov    bx,dx
  84.     mov    dx,ax
  85.     sub    ax,ax
  86.     sar    dx,1
  87.     rcr    ax,1
  88.     sar    dx,1
  89.     rcr    ax,1
  90.     idiv    bx
  91. ;
  92.     ret
  93. @fdiva    ENDP
  94. ;
  95.     PUBLIC _fdiva
  96. _fdiva    PROC  far
  97. ;
  98. ; int far fdiva (int x, int y);
  99. ;
  100. ;    compute (x/y) as a fraction: (a<<14)/y.
  101. ;
  102. px    EQU    ss:WORD PTR[BX+4]
  103. py    EQU    ss:WORD PTR[BX+6]
  104. ;
  105.     mov    bx,sp
  106. ;
  107.     mov    dx,px
  108.     sub    ax,ax
  109.     sar    dx,1
  110.     rcr    ax,1
  111.     sar    dx,1
  112.     rcr    ax,1
  113.     idiv    py
  114. ;
  115.     ret
  116. _fdiva    ENDP
  117. ;
  118.     PUBLIC @muldiva
  119. @muldiva    PROC  far
  120. ;
  121. ; int far _fastcall muldiva (int x, int y, int z); [ax, dx, bx]
  122. ;
  123. ;    compute (x*y)/z
  124. ;
  125.     imul    dx
  126.     idiv    bx
  127. ;
  128.     ret
  129. @muldiva    ENDP
  130. ;
  131.     PUBLIC _muldiva
  132. _muldiva    PROC  far
  133. ;
  134. ; int far muldiva (int x, int y, int z);
  135. ;
  136. ;    compute (x*y)/z
  137. ;
  138. px    EQU    ss:WORD PTR[BX+4]
  139. py    EQU    ss:WORD PTR[BX+6]
  140. pz    EQU    ss:WORD PTR[BX+8]
  141. ;
  142.     mov    bx,sp
  143. ;
  144.     mov    ax,px
  145.     imul    py
  146.     idiv    pz
  147. ;
  148.     ret
  149. _muldiva    ENDP
  150. ;
  151.     PUBLIC @dithadja
  152. @dithadja    PROC  far
  153. ;
  154. ; int far _fastcall dithadja (int x, int dither, int interval);
  155. ;
  156. ;    compute (x*interval+-dither)/1000
  157. ;
  158.     mov    cx,dx
  159.     imul    bx
  160.     or    dx,dx
  161.     js    ditha1
  162.     add    ax,cx
  163.     adc    dx,0
  164.     mov    bx,1000
  165.     idiv    bx
  166.     ret
  167. ditha1:
  168.     sub    ax,cx
  169.     sbb    dx,0
  170.     mov    bx,1000
  171.     idiv    bx
  172.     ret
  173. @dithadja    ENDP
  174. ;
  175.     PUBLIC _dithadja
  176. _dithadja    PROC  far
  177. ;
  178. ; int far dithadja (int x, int dither, int interval);
  179. ;
  180. ;    compute (x*interval+-dither)/1000
  181. ;
  182. px    EQU    ss:WORD PTR[BX+4]
  183. pd    EQU    ss:WORD PTR[BX+6]
  184. pi    EQU    ss:WORD PTR[BX+8]
  185. ;
  186.     mov    bx,sp
  187. ;
  188.     mov    ax,px        ; x
  189.     imul    pi        ; x*interval
  190.     or    dx,dx
  191.     js    ditha
  192.     add    ax,pd        ; x*interval+dither
  193.     adc    dx,0
  194.     mov    bx,1000
  195.     idiv    bx        ; (x*interval+dither)/1000
  196.     ret
  197. ditha:
  198.     sub    ax,pd        ; x*interval-dither
  199.     sbb    dx,0
  200.     mov    bx,1000
  201.     idiv    bx        ; (x*interval-dither)/1000
  202.     ret
  203. _dithadja    ENDP
  204. ;
  205.     PUBLIC _Vmula
  206. _Vmula    PROC  far
  207. ;
  208. ; void far Vmula (int R[3], int V[3], int M[3][3]);
  209. ;
  210. ;    R[X]=fmul(V[X],M[0][X])+fmul(V[Y],M[1][X])+fmul(V[Z],M[2][X]);
  211. ;    R[Y]=fmul(V[X],M[0][Y])+fmul(V[Y],M[1][Y])+fmul(V[Z],M[2][Y]);
  212. ;    R[Z]=fmul(V[X],M[0][Z])+fmul(V[Y],M[1][Z])+fmul(V[Z],M[2][Z]);
  213. ;
  214. ;
  215. pr    EQU    WORD PTR[BP+6]
  216. prb    EQU    WORD PTR[BP+8]
  217. pv    EQU    WORD PTR[BP+10]
  218. pvb    EQU    WORD PTR[BP+12]
  219. pm    EQU    WORD PTR[BP+14]
  220. pmb    EQU    WORD PTR[BP+16]
  221. ;
  222.     push    bp
  223.     mov    bp,sp
  224.     push    si
  225.     push    di
  226.     push    ds
  227.     push    es
  228. ;
  229.     mov    bx,pr        ; r: ds:bx
  230.     mov    si,pv        ; v: es:si
  231.     mov    ax,pvb
  232.     mov    es,ax        ; v base
  233.     mov    di,pm        ; m: ds:di
  234.     mov    ax,pmb
  235.     mov    ds,ax        ; m base
  236. ;
  237.     fmulm    es:0[si],0[di]    ; v0*m00
  238.     mov     cx,dx
  239.     fmulm    es:2[si],6[di]    ; v1*m10
  240.     add     cx,dx
  241.     fmulm    es:4[si],12[di]    ; v2*m20
  242.     add     cx,dx
  243. ;
  244.     mov    dx,ds        ; save ds
  245.     mov    ax,prb
  246.     mov    ds,ax        ; r base
  247.     mov    0[bx],cx    ; r0
  248.     mov    ds,dx        ; restore ds
  249. ;
  250.     fmulm    es:0[si],2[di]    ; v0*m01
  251.     mov     cx,dx
  252.     fmulm    es:2[si],8[di]    ; v1*m11
  253.     add     cx,dx
  254.     fmulm    es:4[si],14[di]    ; v2*m21
  255.     add     cx,dx
  256. ;
  257.     mov    dx,ds        ; save ds
  258.     mov    ax,prb
  259.     mov    ds,ax        ; r base
  260.     mov    2[bx],cx    ; r1
  261.     mov    ds,dx        ; restore ds
  262. ;
  263.     fmulm    es:0[si],4[di]    ; v0*m02
  264.     mov     cx,dx
  265.     fmulm    es:2[si],10[di]    ; v1*m12
  266.     add     cx,dx
  267.     fmulm    es:4[si],16[di]    ; v2*m22
  268.     add     cx,dx
  269. ;
  270.     mov    ax,prb
  271.     mov    ds,ax        ; r base
  272.     mov    4[bx],cx    ; r2
  273. ;
  274.     pop    es
  275.     pop    ds
  276.     pop    di
  277.     pop    si
  278.     pop    bp
  279.     ret
  280. _Vmula    ENDP
  281. ;
  282.     PUBLIC _matyxz
  283. _matyxz    PROC  far
  284. ;
  285. ; void far matyxz (int far T[3][3], int sx, int cx, int sy, int cy,
  286. ;    int sz, int cz)
  287. ;
  288. ;    Build the orientation matrix for the three basic angles
  289. ;
  290. pt    EQU    WORD PTR[BP+6]
  291. ptb    EQU    WORD PTR[BP+8]
  292. psx    EQU    WORD PTR[BP+10]
  293. pcx    EQU    WORD PTR[BP+12]
  294. psy    EQU    WORD PTR[BP+14]
  295. pcy    EQU    WORD PTR[BP+16]
  296. psz    EQU    WORD PTR[BP+18]
  297. pcz    EQU    WORD PTR[BP+20]
  298. ;
  299.     push    bp
  300.     mov    bp,sp
  301.     push    di
  302.     push    ds
  303. ;
  304.     mov    ax,ptb
  305.     mov    ds,ax
  306.     mov    di,pt        ; T: ds:di
  307. ;
  308. ; T[0][2] = -fmul (sy, cx);        0*3+2=2
  309. ;
  310.     fmulm    psy,pcx
  311.     neg    dx
  312.     mov    2*2[di],dx
  313. ;
  314. ; T[1][0] = -fmul (cx, sz);        1*3+0=3
  315. ;
  316.     fmulm    pcx,psz
  317.     neg    dx
  318.     mov    2*3[di],dx
  319. ;
  320. ; T[1][1] = fmul (cx, cz);        1*3+1=4
  321. ;
  322.     fmulm    pcx,pcz
  323.     mov    2*4[di],dx
  324. ;
  325. ; T[1][2] = sx;                1*3+2=5
  326. ;
  327.     mov    ax,psx
  328.     mov    2*5[di],ax
  329. ;
  330. ; T[2][2] = fmul (cy, cx);        2*3+2=8
  331. ;
  332.     fmulm    pcy,pcx
  333.     mov    2*8[di],dx
  334. ;
  335. ; tt1 = fmul (cy, sz);
  336. ;
  337.     fmulm    pcy,psz
  338.     mov    cx,dx        ; tt1: cx
  339. ;
  340. ; tt2 = fmul (sy, cz);
  341. ;
  342.     fmulm    psy,pcz
  343.     mov    bx,dx        ; tt2: bx
  344. ;
  345. ; T[0][1] = tt1 + fmul (tt2, sx);    0*3+1=1
  346. ;
  347.     fmulm    bx,psx
  348.     add    dx,cx
  349.     mov    2*1[di],dx
  350. ;
  351. ; T[2][0] = tt2 + fmul (tt1, sx);    2*3+0=6
  352. ;
  353.     fmulm    cx,psx
  354.     add    dx,bx
  355.     mov    2*6[di],dx
  356. ;
  357. ; tt3 = fmul (cy, cz);
  358. ;
  359.     fmulm    pcy,pcz
  360.     mov    cx,dx        ; tt3: cx
  361. ;
  362. ; tt4 = fmul (sy, sz);
  363. ;
  364.     fmulm    psy,psz
  365.     mov    bx,dx        ; tt4: bx
  366. ;
  367. ; T[0][0] = tt3 - fmul (tt4, sx);    0*3+0=0
  368. ;
  369.     fmulm    bx,psx
  370.     neg    dx
  371.     add    dx,cx
  372.     mov    2*0[di],dx
  373. ;
  374. ; T[2][1] = tt4 - fmul (tt3, sx);    2*3+1=7
  375. ;
  376.     fmulm    cx,psx
  377.     sub    bx,dx
  378.     mov    2*7[di],bx
  379. ;
  380.     pop    ds
  381.     pop    di
  382.     pop    bp
  383.     ret
  384. _matyxz    ENDP
  385. ;
  386.     PUBLIC _matxyz
  387. _matxyz    PROC  far
  388. ;
  389. ; void far matxyz (int far T[3][3], int spitch, int cpitch, int sroll, 
  390. ;    int croll, int syaw, int cyaw)
  391. ;
  392. ;    Build the orientation matrix for the three basic angles
  393. ;
  394. pt    EQU    WORD PTR[BP+6]
  395. ptb    EQU    WORD PTR[BP+8]
  396. psp    EQU    WORD PTR[BP+10]
  397. pcp    EQU    WORD PTR[BP+12]
  398. psr    EQU    WORD PTR[BP+14]
  399. pcr    EQU    WORD PTR[BP+16]
  400. psy    EQU    WORD PTR[BP+18]
  401. pcy    EQU    WORD PTR[BP+20]
  402. ;
  403.     push    bp
  404.     mov    bp,sp
  405.     push    di
  406.     push    ds
  407. ;
  408.     mov    ax,ptb
  409.     mov    ds,ax
  410.     mov    di,pt        ; T: ds:di
  411. ;
  412. ; T[0][0] = fmul (cyaw, croll);        0*3+0=0
  413. ;
  414.     fmulm    pcy,pcr
  415.     mov    0[di],dx
  416. ;
  417. ; T[0][1] = fmul (syaw, croll);        0*3+1=1
  418. ;
  419.     fmulm    psy,pcr
  420.     mov    2[di],dx
  421. ;
  422. ; T[0][2] = -sroll;            0*3+2=2
  423. ;
  424.     mov    ax,psr
  425.     neg    ax
  426.     mov    4[di],ax
  427. ;
  428. ; tt1 = fmul (syaw, cpitch);
  429. ;
  430.     fmulm    psy,pcp
  431.     mov    cx,dx        ; tt1: cx
  432. ;
  433. ; tt2 = fmul (cyaw, spitch);
  434. ;
  435.     fmulm    pcy,psp
  436.     mov    bx,dx        ; tt2: bx
  437. ;
  438. ; T[1][0] = fmul (tt2, sroll) - tt1;    1*3+0=3
  439. ;
  440.     fmulm    bx,psr
  441.     sub    dx,cx
  442.     mov    6[di],dx
  443. ;
  444. ; T[2][1] = fmul (tt1, sroll) - tt2;    2*3+1=7
  445. ;
  446.     fmulm    cx,psr
  447.     sub    dx,bx
  448.     mov    14[di],dx
  449. ;
  450. ; tt3 = fmul (cyaw, cpitch);
  451. ;
  452.     fmulm    pcy,pcp
  453.     mov    cx,dx        ; tt3: cx
  454. ;
  455. ; tt4 = fmul (syaw, spitch);
  456. ;
  457.     fmulm    psy,psp
  458.     mov    bx,dx        ; tt4: bx
  459. ;
  460. ; T[1][1] = fmul (tt4, sroll) + tt3;    1*3+1=4
  461. ;
  462.     fmulm    bx,psr
  463.     add    dx,cx
  464.     mov    8[di],dx
  465. ;
  466. ; T[2][0] = fmul (tt3, sroll) + tt4;    2*3+0=6
  467. ;
  468.     fmulm    cx,psr
  469.     add    dx,bx
  470.     mov    12[di],dx
  471. ;
  472. ;
  473. ; T[1][2] = fmul (croll, spitch);    1*3+2=5
  474. ;
  475.     fmulm    pcr,psp
  476.     mov    10[di],dx
  477. ;
  478. ; T[2][2] = fmul (cpitch, croll);    2*3+2=8
  479. ;
  480.     fmulm    pcp,pcr
  481.     mov    16[di],dx
  482. ;
  483.     pop    ds
  484.     pop    di
  485.     pop    bp
  486.     ret
  487. _matxyz    ENDP
  488. ;
  489.     PUBLIC    _my_sina
  490.     PUBLIC    @my_sina
  491. _my_sina PROC  far
  492. ;
  493. ; short far my_sina (short d);
  494. ;
  495. dangle    EQU    ss:WORD PTR[BX+4]
  496. ;
  497.     mov    bx,sp
  498.     mov    ax,dangle
  499. ;
  500. @my_sina:
  501. ;
  502. ; short far _fastcall my_sina (short d); [ax]
  503. ;
  504. ;    compute sin(d). All conditionals replaced with linear logic.
  505. ;
  506.     mov    cx,ax
  507.     add    cx,cx            ; get sign into carry
  508.     sbb    cx,cx            ; fill reg with carry
  509.     xor    ax,cx            ; negate if -ve
  510.     sub    ax,cx            ;
  511.     jo     l2            ; was D180
  512. ;
  513. ; Now 'ax' is in the positive half (0...180)
  514. ;
  515.     cmp    ax,4000H
  516.     jl     l1
  517.     sub    ax,8000H
  518.     neg    ax
  519. ;
  520. ; Now 'ax' is in the first quarter (0...90).
  521. ;
  522. l1:    push    si
  523.     mov    si,ax
  524.     mov    dx,si            ; f = d&15;
  525.     and    dx,0FH            ;
  526.     sar    si,3            ; d >>= 4;
  527.     and    si,0FFFEH            ;
  528.     mov    bx,_sin_tab[si]        ; l = sin_tab[d];
  529.     mov    ax,_sin_tab[si+2]    ; h = sin_tab[d+1] - l;
  530.     sub    ax,bx            ;
  531.     imul    dx            ; l += (8+h*f)>>4;
  532.     add    ax,8            ;
  533.     sar    ax,4            ;
  534.     add    ax,bx            ;
  535. ;
  536. ; Adjust for sign and return.
  537. ;
  538.     xor    ax,cx            ; return ((short)((l^ind)-ind));
  539.     sub    ax,cx            ;
  540.     pop    si            ;
  541.     ret                ;
  542. l2:    xor    ax,ax            ; return (0);
  543.     ret                ;
  544. _my_sina ENDP
  545. ;
  546.  
  547. ;SRI-UNIX:~billw/cksum.a86 16-nov-84 0400 edit by William Westfield
  548. ; improved checksum algorithm (untested)
  549. ;  Copyright 1984 by the Massachusetts Institute of Technology  
  550. ;  See permission and disclaimer notice in file "notice.h"  
  551.  
  552.     PUBLIC    _chksum
  553. _chksum    PROC  far
  554.  
  555. ; _chksum(buf, len, 0) performs an Internet type checksum. buf points to where
  556. ;     the checksumming should begin, len is the number of 16 bit words
  557. ;    to be summed. Returns the checksum. This is the Unix compatible
  558. ;       version
  559.  
  560.     push    bp
  561.     mov    bp,sp
  562.     push    si
  563.     push    ds
  564.  
  565. ;    mov    bx,4[bp]        ; get buf
  566. ;    mov    cx,6[bp]        ; get len
  567. ;    xor    ax,ax            ; initial value for xsum, clear carry
  568. ;
  569. ;lpchk:    add    ax,[bx]        ; add value and carry    2 bytes, 18 cycles
  570. ;    adc    ax,0        ;            3 bytes,  8 c
  571. ;    inc    bx        ; bump pointer        1 byte,   2 c
  572. ;    inc    bx        ;            1 byte,   2 c
  573. ;    loop    lpchk        ; do it again        2 bytes, 17 c
  574.  
  575. ;new (faster?) checksum algorithm by Bill Westfield
  576. ; The idea is to overlap the carry wrap around with the addition of the
  577. ; next word of data.  Thus we must be careful that pointer increments
  578. ; and looping instructions do not affect the carry flag...
  579.  
  580.     mov    si,8[bp]    ; SEG(buf)
  581.     mov    ds,si
  582.     mov    si,6[bp]    ; OFF(buf)
  583.     mov    cx,10[bp]    ; len
  584.     xor    bx,bx        ; initial value for xsum, clear carry
  585.  
  586. lpchk:    lodsw            ;get next word        1 byte, 16 cycles
  587.     adc    bx,ax        ;overlap adding last CY    2 bytes, 3 c
  588.     loop    lpchk        ;next word        2 bytes, 17 c
  589.  
  590.     mov    ax,bx        ; put where results have to go
  591.     adc    ax,0        ; add final carry bit.
  592.  
  593. contck:
  594.     pop    ds
  595.     pop    si
  596.     pop    bp
  597.     ret                ; go home
  598.  
  599. _chksum    ENDP
  600.     END
  601.